package com.yystudy.system.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yystudy.model.system.SysUser;
import com.yystudy.model.vo.RouterVo;
import com.yystudy.model.vo.SysUserQueryVo;
import com.yystudy.system.mapper.SysUserMapper;
import com.yystudy.system.service.SysMenuService;
import com.yystudy.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author yystudy
 * @since 2023-10-12
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    private SysMenuService sysMenuService;
    /**
     * 条件分页查询
     * @param pageParam
     * @param sysUserQueryVo
     * @return
     */
    @Override
    public IPage<SysUser> selectPage(Page<SysUser> pageParam, SysUserQueryVo sysUserQueryVo) {
        return baseMapper.selectPage(pageParam,sysUserQueryVo);
    }


    /**
     * 更改用户状态
     * @param id
     * @param status
     */
    @Override
    public void updateStatusById(Long id, Integer status) {
        SysUser sysUser = baseMapper.selectById(id);
        sysUser.setStatus(status);
        baseMapper.updateById(sysUser);
    }


    @Override
    public SysUser getByUsername(String username) {
        LambdaQueryWrapper<SysUser> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SysUser::getUsername,username);
        SysUser sysUser = baseMapper.selectOne(lqw);
        return sysUser;
    }


    /**
     * 登录用户信息接口
     * 用户名，用户角色，用户头衔
     * 用户的菜单权限，用户的按钮权限
     * @param username
     * @return
     */
    @Override
    public Map<String, Object> getUserInfo(String username) {
        SysUser sysUser = this.getByUsername(username);
        List<RouterVo> list = sysMenuService.getUserMenuList(sysUser.getId());

        List<String> permsList = sysMenuService.getUserButtonList(sysUser.getId());

        Map<String, Object> map = new HashMap<>();
        map.put("roles","[\"admin\"]");
        map.put("name",username);
        map.put("avatar","https://oss.aliyuncs.com/aliyun_id_photo_bucket/default_handsome.jpg");
        //可以操作的按钮操作权限
        map.put("buttons", permsList);
        //可以操作的菜单导航栏路由权限
        map.put("routers", list);
        return map;
    }

}
